package com.toubutu.audio.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toubutu.audio.admin.dto.AdminUserQueryDto;
import com.toubutu.audio.admin.entity.User;
import com.toubutu.audio.admin.mapper.UserMapper;
import com.toubutu.audio.admin.service.RoleService;
import com.toubutu.audio.admin.service.UserService;
import com.toubutu.audio.admin.vo.AdminUserVo;
import com.toubutu.audio.admin.vo.RoleVo;
import com.toubutu.common.PageUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戒燥
 * @since 2023-09-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private RoleService roleService;
    @Override
    public PageUtils<AdminUserVo> pageQueryAdminUser(AdminUserQueryDto userQueryDto) {
        LambdaQueryWrapper<User> wrapper = userQueryDto.getWrapper();
        Page<User> page = userQueryDto.getPage();
        page = page(page, wrapper);
        List<AdminUserVo> collect = page.getRecords().stream().map(u -> {
            AdminUserVo adminUserVo = new AdminUserVo();
            BeanUtils.copyProperties(u, adminUserVo);
            List<RoleVo> userRoles = roleService.getUserRoles(adminUserVo.getUserId());
            adminUserVo.setRoleList(userRoles);
            return adminUserVo;
        }).collect(Collectors.toList());
        return new PageUtils<>(collect,page);
    }
}
